<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class RegAction extends Action {

	//登录验证
	public function index() {
		$this->assign("code",$_GET["code"]);
		$this->display();
	}

	//提交注册
	public function save() {
		$sex = $_POST["sex"];
		$email = trim($_POST["email"]);
		$password = $_POST["password"];
		$repassword = $_POST["repassword"];
		$invite_code = trim($_POST["invite_code"]);

		if ($sex == '')
			$this->ajaxReturn(null, array('sex' => '性别不能为空'), -4);
		if (!empty($email) && !Model::regex($email, "email"))
			$this->ajaxReturn(null, array('emial' => "邮箱格式错误"), -1);
		$password != $repassword && $this->ajaxReturn(null, array('password' => "两次密码不一致"), -2);
		empty($invite_code) && $this->ajaxReturn(null, array('invite_code' => "请填写邀请码"), -3);


		$invite = M("invite")->where("invite_id='" . $invite_code . "'")->find();
		if ($invite) {
			if ($invite['status'] == 0) {
				$User = M("User");
				$user = $User->where("email='" . $email . "'")->find();
				if ($user) {
					$this->ajaxReturn(null, array('email' => "账号已存在"), -4);
				} else {
					$User->email = $email;
					$User->password = md5($password);
					$User->create_time = time();
					$User->parent_id = $invite["user_id"];
					$User->status = 1;
					$new_user_id = $User->add();
					M("invite")->where("invite_id='" . $invite_code . "'")->save(array(
						"reg_user_id" => $new_user_id,
						"status" => 1,
					));
					if ($new_user_id) {
						$this->ajaxReturn(null, "注册成功", 1);
					} else {
						$this->ajaxReturn(null, "发生异常", -7);
					}
				}
			} else {
				$this->ajaxReturn(null, array('invite_code' => "邀请码不能重复使用"), -5);
			}
		} else {
			$this->ajaxReturn(null, array('invite_code' => "邀请码不存在"), -6);
		}
	}

	//身份验证方式注册
	public function auth() {
		$schools = D("School")->cache("school")->where("status=1")->select();
		$this->assign("schools", $schools);
		$this->display();
	}

	//提交验证信息
	public function saveAuth() {
		$data['school_id'] = (int) $_POST["school_id"];
		$data['realname'] = $_POST["realname"];
		$data['s_number'] = $_POST["s_number"];
		$data['photo[]'] = $_FILES["photo"];
		$this->assign("waitSecond", -1);

		$data['school_id'] == 0 && $this->error("请选择所在学校");
		empty($data['realname']) && $this->error("请提供您的真实姓名，以方便我们核实");
		empty($data['s_number']) && $this->error("请提供您的学号信息");
		!$data['photo[]'] && $this->error("请提供一张学生证的照片");

		$profile = M("Profile")->where("school_id=" . $data["school_id"] . " and s_number='" . $data["s_number"] . "'")->find();
		$profile && $this->error("学号已存在，你是否已经注册过账号？<a href=\"" . __GROUP__ . "\">尝试登陆</a>");

		import("@.ORG.Net.UploadFile");
		import("@.ORG.Util.Image");
		$upload = new UploadFile(); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->allowTypes = array('image/jpg', 'image/gif', 'image/png', 'image/jpeg', 'image/bmp'); // 设置附件上传类型
		$upload->savePath = './attachments/profile/' . $data["school_id"] . "/"; // 设置附件上传目录
		$upload->saveRule = time;
		$upload->thumb = true;
		$upload->thumbPrefix = "p";
		$upload->thumbMaxWidth = 800;
		$upload->thumbMaxHeight = 800;
		$upload->thumbRemoveOrigin = true;

		if (!$upload->upload()) { // 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		} else { // 上传成功 获取上传文件信息
			$photos = $upload->getUploadFileInfo();
		}
		if ($photos) {
			$data['photo'] = $photos[0]['savename'];
			count($photos) > 1 && $data['photo2'] = $photos[1]['savename'];
		}
		$data['create_time'] = time();
		M("Profile")->add($data);
		$this->success("提交成功，我们将尽快给您邮件回复，请注意查收。", "javascript:;");
	}

}

?>
